﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLKHACHHANG.DataAccess;


namespace QLKHACHHANG
{
    public partial class frmCustomer : DevExpress.XtraEditors.XtraForm
    {
        public frmCustomer()
        {
            InitializeComponent();
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            frmCustomerConfig cusconfig = new frmCustomerConfig(this,false);
            cusconfig.SetTitle("Thêm khách hàng");           
            cusconfig.ShowDialog();
        }

        private void btMod_Click(object sender, EventArgs e)
        {
            if (gvKhachhang.FocusedRowHandle >= 0)
            {
                frmCustomerConfig cusconfig = new frmCustomerConfig(this, true);
                cusconfig.SetTitle("Sửa thông tin khách hàng");
                cusconfig.ShowDialog();
            }
            else
            {
                MessageBox.Show("Vui lòng chọn dòng khách hàng cần sửa!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void frmCustomer_Load(object sender, EventArgs e)
        {            
           
            // TODO: This line of code loads data into the 'ds_KhachHang.vi_LayThongTinKH' table. You can move, or remove it, as needed.
            try
            {
                this.vi_LayThongTinKHTableAdapter.Connection.ConnectionString = SetConfig.getAppSetting("ConnectionString");
                this.vi_LayThongTinKHTableAdapter.Fill(this.ds_KhachHang.vi_LayThongTinKH);                
                gvKhachhang.FocusedRowHandle = -999997;            
                foreach (Control c in this.Controls)
                    MessageBox.Show(c.ToString());  
            }
            catch (Exception ex)
            {
                //DevExpress.XtraSplashScreen.SplashScreenManager.CloseForm();
                MessageBox.Show("Vui lòng kiểm tra lại ! \n\n (" + ex.Message + ")", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
           
        }
        public void UpdateView()
        {
            this.vi_LayThongTinKHTableAdapter.Fill(this.ds_KhachHang.vi_LayThongTinKH);
            gvKhachhang.FocusedRowHandle = 0;
        }
        private void gvKhachhang_DoubleClick(object sender, EventArgs e)
        {
            if (gvKhachhang.FocusedRowHandle >= 0)
            {
                frmCustomerConfig cusconfig = new frmCustomerConfig(this, true);
                cusconfig.SetTitle("Sửa thông tin khách hàng");
                cusconfig.ShowDialog();             
            }
            
        }

        private void btDel_Click(object sender, EventArgs e)
        {
            if (gvKhachhang.FocusedRowHandle >= 0)
            {
                if (MessageBox.Show("Bạn có muốn xóa khách hàng có mã: " + gvKhachhang.GetFocusedDataRow()["Makh"].ToString() + " ?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    DBCustomer.DeleteCustomer(gvKhachhang.GetFocusedDataRow()["Makh"].ToString());
                    this.vi_LayThongTinKHTableAdapter.Fill(this.ds_KhachHang.vi_LayThongTinKH);
                }                  
            }
            else
            {
                MessageBox.Show("Vui lòng chọn dòng khách hàng cần xóa!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
          
        }   


        private void gvKhachhang_RowCountChanged(object sender, EventArgs e)
        {
            txtTotal.Text = gvKhachhang.RowCount.ToString();
       
        }
        
        
    }
}